﻿using System.Collections.Generic;
using System.Windows;
using Rio.Manager.Application.Parsers;
using Rio.Manager.Application.ViewModel.FolderTypeAdministration;
using Telerik.Windows.Controls;
using Telerik.Windows.Documents.Layout;
using Telerik.Windows.Documents.Model;
using System.Text.RegularExpressions;
using Telerik.Windows.Documents.FormatProviders.Xaml;
using Telerik.Windows.Documents.FormatProviders.Txt;

namespace Rio.Manager.Application.Controls
{
    public partial class WorkflowRulesEditorChildWindow
    {
        private RadDocument XamlDocument;

        public string Rules { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowRulesEditorChildWindow" /> class.
        /// </summary>
        public WorkflowRulesEditorChildWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the OkBtn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void OkBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;

            var provider = new TxtFormatProvider();
            Rules = provider.Export(RulesEditor.Document);
            Close();
        }

        /// <summary>
        /// Handles the Click event of the CancelBtn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        /// <summary>
        /// Loads the rules document.
        /// </summary>
        /// <param name="rules">The rules.</param>
        public void LoadRulesDocument(string rules)
        {
            if (this.XamlDocument == null)
            {
               this.XamlDocument = CreateFormattedDocument(rules, ".xaml");
            }

            this.RulesEditor.Document = this.XamlDocument;
            this.RulesEditor.UpdateEditorLayout();
        }

        /// <summary>
        /// Creates the formatted document.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="fileFormat">The file format.</param>
        /// <returns></returns>
        private RadDocument CreateFormattedDocument(string text, string fileFormat)
        {
            RadDocument document = new RadDocument();
            document.LayoutMode = DocumentLayoutMode.Flow;
            document.SectionDefaultPageMargin = new Padding(25);

            Section section = new Section();
            document.Sections.Add(section);

            if (!text.Contains("\r\n"))
                text = text.Replace("\n", "\r\n");
            Tokenizer tokenizer = new Tokenizer();
            List<Token> tokens = tokenizer.TokenizeCode(text, fileFormat);

            Paragraph currentParagraph = new Paragraph();
            currentParagraph.SpacingAfter = 0;
            section.Blocks.Add(currentParagraph);
            foreach (Token token in tokens)
            {
                string[] lines = Regex.Split(token.Value, DocumentEnvironment.NewLine);

                bool createParagraph = false;
                foreach (string line in lines)
                {
                    if (createParagraph)
                    {
                        currentParagraph = new Paragraph();
                        currentParagraph.SpacingAfter = 0;
                        section.Blocks.Add(currentParagraph);
                    }
                    createParagraph = true;

                    if (!string.IsNullOrEmpty(line))
                    {
                        Span span = token.GetSpanStyle();
                        span.FontSize = 12;
                        span.Text = line;
                        currentParagraph.Inlines.Add(span);
                    }
                }
            }

            return document;
        }


    }
}
